
package com.joysuch.wwyt.core.common.util;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;

import org.springframework.beans.BeanUtils;

public class PropertyUtils {

    public static <T> T extractValue(Object bean, String property, Class<T> clazz) {
        if (bean == null) {
            return null;
        }
        PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(bean.getClass(), property);
        if (descriptor != null && descriptor.getReadMethod() != null) {
            try {
                return (T) descriptor.getReadMethod().invoke(bean, new Object[0]);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
